package cn.iocoder.yudao.module.crm.controller.admin.fuelingrecord;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.crm.controller.admin.fuelingrecord.vo.*;
import cn.iocoder.yudao.module.crm.dal.dataobject.fuelingrecord.FuelingRecordDO;
import cn.iocoder.yudao.module.crm.service.fuelingrecord.FuelingRecordService;

@Tag(name = "管理后台 - 加油记录")
@RestController
@RequestMapping("/crm/fueling-record")
@Validated
public class FuelingRecordController {

    @Resource
    private FuelingRecordService fuelingRecordService;

    @PostMapping("/create")
    @Operation(summary = "创建加油记录")
    @PreAuthorize("@ss.hasPermission('crm:fueling-record:create')")
    public CommonResult<Long> createFuelingRecord(@Valid @RequestBody FuelingRecordSaveReqVO createReqVO) {
        return success(fuelingRecordService.createFuelingRecord(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新加油记录")
    @PreAuthorize("@ss.hasPermission('crm:fueling-record:update')")
    public CommonResult<Boolean> updateFuelingRecord(@Valid @RequestBody FuelingRecordSaveReqVO updateReqVO) {
        fuelingRecordService.updateFuelingRecord(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除加油记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('crm:fueling-record:delete')")
    public CommonResult<Boolean> deleteFuelingRecord(@RequestParam("id") Long id) {
        fuelingRecordService.deleteFuelingRecord(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得加油记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('crm:fueling-record:query')")
    public CommonResult<FuelingRecordRespVO> getFuelingRecord(@RequestParam("id") Long id) {
        FuelingRecordDO fuelingRecord = fuelingRecordService.getFuelingRecord(id);
        return success(BeanUtils.toBean(fuelingRecord, FuelingRecordRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得加油记录分页")
    @PreAuthorize("@ss.hasPermission('crm:fueling-record:query')")
    public CommonResult<PageResult<FuelingRecordRespVO>> getFuelingRecordPage(@Valid FuelingRecordPageReqVO pageReqVO) {
        PageResult<FuelingRecordDO> pageResult = fuelingRecordService.getFuelingRecordPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, FuelingRecordRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出加油记录 Excel")
    @PreAuthorize("@ss.hasPermission('crm:fueling-record:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportFuelingRecordExcel(@Valid FuelingRecordPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<FuelingRecordDO> list = fuelingRecordService.getFuelingRecordPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "加油记录.xls", "数据", FuelingRecordRespVO.class,
                        BeanUtils.toBean(list, FuelingRecordRespVO.class));
    }

}